<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <!-- header -->
        <div>
          <el-row type="flex" justify="space-between">
            <el-col :span="12">
          <p class="pink">说明：目前支持学科和关键字条件筛选</p>
            </el-col>
            <el-col :span="2" style="margin-top:10px">
                <el-button size="mini" type="success" class="btn" @click="$router.push('new')">
            <i class="el-icon-edit"></i>
            <span>新增试题</span>
          </el-button>
            </el-col>
          </el-row>

          <el-form label-width="80px">
            <el-row>
              <el-col :span="6">
                <el-form-item label="学科">
                  <el-select
                    size="small"
                    style="width: 100%"
                    v-model="form.subjectID"
                    @change="subjectChange"
                    placeholder="请选择"
                  >
                    <el-option
                      v-for="item in subjectList"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="二级目录">
                  <el-select
                    size="small"
                    style="width: 100%"
                    v-model="form.catalogID"
                    placeholder="请选择"
                  >
                    <el-option
                      v-for="item in directoryList"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="标签">
                  <el-select
                    size="small"
                    style="width: 100%"
                    v-model="form.tags"
                    placeholder="请选择"
                  >
                    <el-option
                      v-for="item in tagSimpleList"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="关键字">
                  <el-input
                    v-model="form.keyword"
                    style="width: 100%"
                    size="small"
                    placeholder="根据题干搜索"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="试题类型">
                  <el-select
                    v-model="form.questionType"
                    size="small"
                    style="width: 100%"
                    placeholder="请选择"
                  >
                    <el-option
                      v-for="item in questionType"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="难度">
                  <el-select
                    size="small"
                    style="width: 100%"
                    v-model="form.difficulty"
                    placeholder="请选择"
                  >
                    <el-option
                      v-for="item in difficulty"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="方向">
                  <el-select
                    v-model="form.direction"
                    size="small"
                    style="width: 100%"
                    placeholder="请选择"
                  >
                    <el-option
                      v-for="item in direction"
                      :key="item"
                      :label="item"
                      :value="item"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="录入人">
                  <el-select
                    v-model="form.creatorID"
                    size="small"
                    style="width: 100%"
                    placeholder="请选择"
                  >
                    <el-option
                      v-for="(item,index) in entryStaff"
                      :key="index"
                      :label="item"
                      :value="index"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="题目备注">
                  <el-input
                    v-model="form.remarks"
                    style="width: 100%"
                    size="small"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="企业简称">
                  <el-input  v-model="form.shortName" style="width: 100%" size="small"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="城市">
                  <el-select
                  @change="provinceChange"
                  v-model="form.province"
                    size="small"
                    style="width: 50%"
                    placeholder="请选择"
                  >
                    <el-option
                    v-for="item in provinceList"
                    :key="item"
                    :label="item"
                    :value="item"
                    ></el-option>
                  </el-select>
                  <el-select
                    v-model="form.city"
                    size="small"
                    style="width: 50%"
                    placeholder="请选择"
                  >
                    <el-option
                    v-for="item in cityList"
                    :key="item"
                    :label="item"
                    :value="item"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="3" :push="3">
                <el-button>清除</el-button>
                <el-button type="primary" @click="searchList">搜索</el-button>
              </el-col>
            </el-row>
          </el-form>
        </div>
        <!-- counts -->
        <div class="gray">
          <p class="font">
            <i class="el-icon-info"></i>
            <span
              >数据一共
              <span>{{ pageParams.counts }}</span>
              条
            </span>
          </p>
        </div>
        <!-- table -->
        <el-table :data="list" style="width: 100%">
          <el-table-column
            prop="number"
            label="试题编号"
            width="100"
          ></el-table-column>
          <el-table-column
            prop="subject"
            label="学科"
            width="100"
          ></el-table-column>
          <el-table-column prop="catalog" label="目录" width="100">
          </el-table-column>
          <el-table-column prop="questionType" label="题型" width="100">
            <template v-slot="{ row }">
              <span>{{
                row.questionType === "1"
                  ? "单选"
                  : row.questionType === "2"
                  ? "多选"
                  : "简答"
              }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="question" label="题干" width="300">
            <template v-slot="{ row }">
              <span v-html="row.question"></span>
            </template>
          </el-table-column>
          <el-table-column prop="addDate" label="录入时间" width="150">
            <template v-slot="{ row }">
              <span>{{ timeCycle(row.addDate) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="difficulty" label="难度" width="100">
             <template v-slot="{ row }">
              <span>{{
                row.difficulty === "1"
                  ? "简单"
                  : row.questionType === "2"
                  ? "一般"
                  : "困难"
              }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="creator" label="录入人" width="100">
          </el-table-column>

          <el-table-column label="操作">
             <template v-slot="{ row }">
            <el-button
            @click="preview(row.id)"
              circle
              icon="el-icon-view"
              type="primary"
              plain
            ></el-button>
            <el-button
            @click="$router.push(`new?id=${row.id}`)"
              circle
              icon="el-icon-edit"
              type="success"
              plain
            ></el-button>
            <el-button
            @click="deleteOk(row.id)"
              circle
              icon="el-icon-delete"
              type="danger"
              plain
            ></el-button>
            <el-button
            @click="addFeaturedTopics(row.id)"
              circle
              icon="el-icon-check"
              type="warning"
              plain
            ></el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
          class="el-pagination"
          background
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
          :current-page="pageParams.page"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="pageParams.pagesize"
          layout="  prev, pager, next,sizes, jumper"
          :total="pageParams.counts"
        >
        </el-pagination>
      </el-card>
      <!-- 预览框  -->
      <QuestionPreview ref="QuestionPreview" :dialogVisible.sync="dialogVisible"></QuestionPreview>
    </div>
  </div>
</template>

<script>
import QuestionPreview from '../components/questions-preview'
import moment from 'moment'
import { list, remove, choiceAdd } from '@/api/hmmm/questions.js'
import { simple } from '@/api/hmmm/subjects.js'
import { simple as directorysSimple } from '@/api/hmmm/directorys.js'
import { simple as tagsSimple } from '@/api/hmmm/tags.js'
import {
  questionType,
  difficulty,
  direction,
  entryStaff
} from '@/api/hmmm/constants.js'
import { provinces, citys } from '@/api/hmmm/citys'
export default {
  components: {
    QuestionPreview
  },
  data () {
    return {
      dialogVisible: false,
      list: [],
      pageParams: {
        page: 1,
        pagesize: 5,
        counts: 0
      },
      form: {
        page: 1,
        pagesize: 5,
        subjectID: '', // 学科
        catalogID: '', // 二级目录
        tags: '', // 标签
        keyword: '', // 关键字
        questionType: '', // 试题类型
        difficulty: '', // 难度
        direction: '', // 方向
        creatorID: '', // 录入人
        remarks: '', // 题目备注
        shortName: '', // 企业简称
        province: '', // 城市
        city: '' // 区县

      },
      subjectList: [], // 学科
      directoryList: [], // 二级目录
      tagSimpleList: [], // 标签
      questionType, // 试题类型
      difficulty, // 难度
      direction, // 方向
      entryStaff, // 录入人
      provinceList: [], // 城市
      cityList: [] // 区县
    }
  },
  created () {
    this.listData()
    this.subjectsSimple()
    // this.directorysSimple()
    this.provincesList()
  },
  methods: {
    // 获取列表数据
    async listData () {
      const { data } = await list(this.pageParams)
      // console.log(data)
      // console.log(data.items)
      // console.log(data.counts)
      this.list = data.items
      this.pageParams.counts = data.counts
    },
    // 页面切换渲染
    handleCurrentChange (val) {
      // console.log(val)
      this.pageParams.page = val
      this.listData()
    },
    // 切换条数渲染
    handleSizeChange (val) {
      // console.log(val)
      this.pageParams.pagesize = val
      this.listData()
    },
    // 时间转换
    timeCycle (time) {
      return moment().format('YYYY-DD-MM hh:mm:ss')
    },
    // 获取学科简单列表
    async subjectsSimple () {
      const { data } = await simple()
      // console.log(data)
      this.subjectList = data
    },
    // 学科改变获取二级目录简单列表  获取标签
    async subjectChange () {
      // console.log(this.form.subjectList)
      const { data } = await directorysSimple({
        subjectID: this.form.subjectID
      })
      console.log(data)
      this.directoryList = data
      const { data: res } = await tagsSimple({
        subjectID: this.form.subjectID
      })
      console.log(res)
      this.tagSimpleList = res
    },
    // 获取城市数据列表
    provincesList () {
      this.provinceList = provinces()
      // console.log(this.provinceList)
    },
    // 获取城市下地区列表
    provinceChange () {
      this.cityList = citys(this.form.province)
      // console.log(this.cityList)
    },
    // 搜索
    async searchList () {
      const { data } = await list(this.form)
      this.list = data.items
      this.counts = data.counts
    },
    // 预览
    preview (id) {
      this.dialogVisible = true
      this.$refs.QuestionPreview.articleDetails(id)
    },
    // 删除
    deleteOk (id) {
      console.log(id)
      this.$confirm('此操作将永久删除该题目, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        if (this.list.length === 1) {
          this.page--
        }
        await remove({ id })
        this.listData()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 添加精选题目
    addFeaturedTopics (id) {
      this.$confirm('此操作将该题目加入精选, 是否继续?, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await choiceAdd({
          choiceState: 1,
          id

        })
        this.listData()
        this.$message({
          type: 'success',
          message: '加入精选成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消添加'
        })
      })
    }
  }
}
</script>

<style scoped>
.btn {
  display: flex;
  float: right;
}
.pink {
  font-size: 12px;
  color: pink;
  margin-bottom: 20px;
}
.gray {
  background-color: #f4f4f5;
  height: 30px;
  line-height: 30px;
  border-radius: 5px;
}
.font {
  font-size: 13px;
  margin: 10px;
  color: #909399;
}
.el-icon-info {
  margin-right: 10px;
}
.el-pagination {
  display: flex;
  float: right;
  margin: 20px;
}
</style>
